package bairui.international.console.web.controller.user.offset;

import java.io.File;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.lianjing.sh.validate.spring.exception.ValidateObjectException;

import bairui.international.common.tool.Util_GenerateQRCode;
import bairui.international.model.domain.jpa.TB_ElectronicTicket;
import bairui.international.service.Service_ElectronicTicket;

/**
 *
 * 分配电子券
 *
 * @generated
 * 
 * @see STest_C_User_Offset_AssignOffset_Assign 测试
 *
 */
@Controller
public class C_User_Offset_AssignOffset_Assign
 extends SC_User_Offset_AssignOffset_Assign 
{
	
//	@Autowired private Config_QR config_QR;
	@Autowired private Service_ElectronicTicket service_ElectronicTicket;
	
	/**
	 * 分配
	 */
	@Action(Assign.class)
	@PostMapping(Assign.RequestMappingValue)
	public @FomatOutput @ResponseBody Object assign(
		@Valid OffsetPage_IdentifierInput input, 
		@PathVariable(name=Assign.PathVariableName.UserId) long userId
	) throws Exception {
		List<Long> ids = input.getId();
		if(ids.isEmpty()){
			throw new ValidateObjectException("Object", "", "选择不能为空");
		}
		service_ElectronicTicket.assign(ids, userId);
		/*
		 * 电子券
		 */
		// 创建文件夹
		if(!new File(pathConfig.getPosition_Image_QR_Ticket()).exists()) {
			new File(pathConfig.getPosition_Image_QR_Ticket()).mkdirs();
		}
		for(Long id : ids) {
			TB_ElectronicTicket bean = service_ElectronicTicket.findById(id);
			if(!bean.getElectronicTicketOrder().getGiveFlag()) {
				continue;
			}
			// 生成点券二维码
			String fileName = Util_GenerateQRCode.generateQRCode(
					this.pathConfig.getPosition_Image_QR_Ticket(), 
					"ticket", 
					String.valueOf(id)
				);
	    	service_ElectronicTicket.setDimensionalUrl(id, fileName);
		}
		return null;
	}
	

}
